package jsyf.oa.service.device;

import com.alibaba.dubbo.common.utils.StringUtils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import jsyf.com.util.FastJsonConvert;
import jsyf.oa.dao.device.DeviceTypeDao;
import jsyf.oa.entity.device.DeviceBrandEntity;
import jsyf.oa.entity.device.DeviceTypeEntity;
import jsyf.oa.facade.device.DeviceTypeFacade;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by y on 2018/6/25.
 */
@Service
@Transactional(rollbackFor = Exception.class)
@com.alibaba.dubbo.config.annotation.Service(protocol = {"dubbo"})
public class DeviceTypeService implements DeviceTypeFacade{
    @Autowired
    private DeviceTypeDao deviceTypeDao;
    @Override
    public JSONObject getDeviceTypeList(String jsonData) {
        JSONObject jsonParams = FastJsonConvert.convertJSONToObject(jsonData, JSONObject.class);
        //分页信息
        int requestPage = 1;
        int pageSize = Integer.MAX_VALUE;

        if (jsonParams.containsKey("pageSize")) {
            pageSize = jsonParams.getInteger("pageSize");
        }
        if (jsonParams.containsKey("requestPage")) {
            requestPage = jsonParams.getInteger("requestPage");
        }
        //传参
        StringBuffer condition = new StringBuffer();
        List<Object> sqlArgs = new ArrayList<Object>();
        condition.append("AND DEL_FLAG=0 order by SORT_NO desc");
        //查询list
        List<JSONObject> records = deviceTypeDao.getJSONListByPage(condition.toString(), sqlArgs.toArray(), requestPage, pageSize);
        //查询总数
        JSONObject root=new JSONObject();
        root.put("name","设备类型");
        root.put("type","root");
        root.put("open","true");
        records.add(root);
        Integer recordCount = deviceTypeDao.getPageCount(condition.toString(), sqlArgs.toArray());

        //分装前端需要的格式
        int pageCount = 0;
        if (recordCount < pageSize) {
            pageCount = 1;
        } else {
            double flag = Math.ceil(recordCount / pageSize + 1);
            pageCount = ((int) flag);
        }
        if (recordCount == 0) {
            pageCount = 0;
        }
        //存放分页信息
        JSONObject sendData = new JSONObject();
        //存放list
        JSONObject result = new JSONObject();
        sendData.put("recordCount", recordCount);
        sendData.put("pageCount", pageCount);
        sendData.put("requestPage", 1);
        result.put("pager", sendData);
        // 子数组对象
        result.put("records", records);
        return result;
    }

    @Override
    public Boolean update(String jsonData) {
        DeviceTypeEntity deviceTypeEntity = JSON.toJavaObject(JSON.parseObject(jsonData), DeviceTypeEntity.class);
        return deviceTypeDao.update(deviceTypeEntity)>0;
    }

    @Override
    public Boolean delById(String jsonData) {
        return deviceTypeDao.delById(JSON.parseObject(jsonData).getString("id")) > 0;
    }

    @Override
    public String add(String jsonObject) {
        JSONObject JSONObject = JSON.parseObject(jsonObject);
        DeviceTypeEntity deviceTypeEntity = JSON.toJavaObject(JSONObject, DeviceTypeEntity.class);
        deviceTypeEntity.setDelFlag(0);
        deviceTypeEntity.setSortNo(0);
        deviceTypeDao.add(deviceTypeEntity);
        return deviceTypeEntity.getId();
    }
}
